package TextEditor.facade;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JTextArea;

/**
 * Implementation of FileInterface to additively cut the selected text
 * @author Yij_Zhan
 *
 */
public class AdditiveCutOperation implements FileInterface {

	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Clipboard clipBoard = toolkit.getSystemClipboard();

	@Override
	public void fileOperation(JTextArea textArea) {
		// TODO Auto-generated method stub
		textArea.requestFocusInWindow();

		Transferable contents = clipBoard.getContents(this);
		String clipboardString = "";

		try {
			clipboardString = (String)contents.getTransferData(DataFlavor.stringFlavor);
		} catch (UnsupportedFlavorException e1) {		
			e1.printStackTrace();
		} catch (IOException e1) {		
			e1.printStackTrace();
		}
		String newCutString = textArea.getSelectedText();
	
		if (newCutString == null) newCutString = "";

		String addCutString = clipboardString.concat(newCutString);

		StringSelection selection = new StringSelection(addCutString);
		clipBoard.setContents(selection,null);
		textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
		
	}

}
